package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.tplus.dto.RefreshTokenReqDto;
import org.jeecg.modules.tplus.service.ITplusService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * T+系统Token刷新定时任务
 * 每两小时执行一次，刷新T+系统的访问token
 *
 * @Author: System
 * @Date: 2025-01-16
 */
@Slf4j
@Component
public class TplusTokenRefreshJob implements Job {

    @Autowired
    private ITplusService tplusService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("开始执行T+系统Token刷新任务，执行时间: {}", DateUtils.now());
        try {
            // 调用getToken方法刷新token
            tplusService.getToken(new RefreshTokenReqDto());

            log.info("T+系统Token刷新任务执行成功，执行时间: {}", DateUtils.now());

        } catch (Exception e) {
            log.error("T+系统Token刷新任务执行失败，执行时间: {}, 错误信息: {}", DateUtils.now(), e.getMessage(), e);
            throw new JobExecutionException("T+系统Token刷新失败", e);
        }
    }
}
